<?php	
   $path = $_SERVER['DOCUMENT_ROOT'];
   $path .= "/Gen_Scripts/database.php";
   include_once($path);

/* Utility to clean incoming data */
function clean_data($string) {
dblogin();
  if (get_magic_quotes_gpc()) {
    $string = stripslashes($string);
  }
  $string = strip_tags($string);
  return mysql_real_escape_string($string);
}

/* function to print out the name of the player */
function player_name($player_id){
	dblogin();
	$query = 'select name from PLAYERS p where p.player_id = '.$player_id;
	 if($result = mysql_query($query)){
		if($row = mysql_fetch_array($result)){
			return $row['name'];
		}
	}
	return '';
}
	
/*************************************************************
                       Game Stuff
 *************************************************************/
/*  Display the link and name of the game assocatied with the gameid  */
function print_gamename($gameid){
	dblogin();
	$query = 
	"select gameid, game from GAMES p where p.gameid = ".$gameid;
	if($result = mysql_query($query)){
		if($row = mysql_fetch_array($result)){
			echo $row["game"],'<br>';
		}
	}
}	

function show_games($type, $num){

	dblogin();
	$unvoted = true;
	$query = "";
	if($type=='PEND'){
		$query = 
		"select g.gameid, g.game, g.owner
		   from GAMES g
		  where g.status = '?';";
	} 
	if($type == 'PLAY'){
		$query = 
		"select g.gameid, g.game, g.owner
		   from GAMES g
		  where g.status = 'P';";
	}
	if($type == 'DONE'){
		$query = 
		"select g.gameid, g.game, g.owner
		   from GAMES g
		  where g.status = 'D';";
	}
	if($type == 'ALL'){
		$query = 
		"select g.gameid, g.game, g.owner
		   from GAMES g;";
	}
	if($type == 'UNVOTE'){
		$query = 
		"select g.gameid, g.game, v.vote, g.owner
		   from GAMES g
		   left outer join GAME_VOTE v
		     on g.gameid = v.gameid
			 and v.player = ".$_SESSION['USERNUM']."
		  where v.vote is null;";
	}
	if($type == 'ME'){
		$query = 
		"select g.gameid, g.game,  g.owner
		   from GAMES g
	      where g.owner = ".$_SESSION['USERNUM'];
	}

	if($result = mysql_query($query)){
		while($row = mysql_fetch_array($result)){
			$unvoted = false;
			$gameid = $row["gameid"];
			$owner = $row["owner"];
			$name = $type.$gameid;
								
	echo '		<div id="game">
					<h1> ',$row['game'],' </h1>';
					


	/***    Get the game's status   ***/				
				$query2 = 
				"select count(p.player_id) as \"TOT_PLAYERS\",
						sum(if(ifnull(g.vote,1) = g.vote, 0,1)) as \"NO_VOTE\",
						sum(if(g.vote = 'Y', 1, 0)) as \"POS_VOTE\",
						sum(if(g.vote = '?', 1, 0)) as \"IDC_VOTE\",
						sum(if(g.vote = 'N', 1, 0)) as \"NEG_VOTE\"
				  from PLAYERS p
				  left outer join 
					(select * from GAME_VOTE g where g.gameid = ".$gameid." )g
					on p.player_id = g.player
				  where p.status = 'G'";
				if($result2 = mysql_query($query2)){
					while($row2 = mysql_fetch_array($result2)){
						$TOT_PLAYERS = $row2['TOT_PLAYERS'];
						$NO_VOTE = $row2['NO_VOTE'];
						$POS_VOTE = $row2['POS_VOTE'];
						$IDC_VOTE = $row2['IDC_VOTE'];
						$NEG_VOTE = $row2['NEG_VOTE'];
					}		
				}		
	echo'			<div id="status">
						<div id="idc" style="width: ',$IDC_VOTE/$TOT_PLAYERS*100,'%;"></div>
						<div id="pos" style="width: ',$POS_VOTE/$TOT_PLAYERS*100,'%;"></div>
						<div id="novote" style="width: ',$NO_VOTE/$TOT_PLAYERS*100,'%;"></div>
						<div id="neg" style="width: ',$NEG_VOTE/$TOT_PLAYERS*100,'%;"></div>
						<div id="bar" style="left: ',(200 + ((200/$TOT_PLAYERS)*$IDC_VOTE))/2,'px;"></div>
					</div>';
					
	/***    Get my vote  ***/				
				$query3 = 
				"select ifnull(g.vote,'NULL') as \"VOTE\"
				  from PLAYERS p
				  left outer join 
				  (select * from GAME_VOTE g where g.gameid = ".$gameid.") g
				    on p.player_id = g.player
				  where p.player_id = ".$_SESSION['USERNUM'];
				if($result3 = mysql_query($query3)){
					while($row3 = mysql_fetch_array($result3)){
						$VOTE = $row3['VOTE'];
					}		
				}	
					
		echo "   <script type=\"text/javascript\">
					$(function() {
						$('.game_vote').click(function(){
							$('#gamelist_ajax').load(\"Games/games.php\",{type: '",$type,"' , game:$(this).attr(\"name\"), vote: $(this).attr(\"value\")}); 
							return false;
						});
					});
				</script>";				
					
	echo '			<div id="pos">
						<input alt="Lets play it!" class="game_vote ';if($VOTE == 'Y'){ echo 'selected'; } echo '" name="',$gameid,'" type="submit" value="+">
					</div>
					<div id="neg">
						<input alt="Nah Don\'t like it"class="game_vote ';if($VOTE == 'N'){ echo 'selected'; } echo '" name="',$gameid,'" type="submit" value="-">
					</div>
					<div id="idc">
						<input alt="Don\'t care either way" class="game_vote ';if($VOTE == '?'){ echo 'selected'; } echo '" name="',$gameid,'" type="submit" value="&empty;">					
					</div>
					<img class="pointer" src="images/icons/discussion.png" alt="Discussion" style=" margin: 4px;" onClick=\'$("#',$name,'").load("Games/game_discussion.php",{gameid:',$gameid,', showtype:"',$type,'"})\' type="button" />
					<img class="pointer" src="images/icons/details.png"    alt="Info" style=" margin: 4px;" onClick=\'$("#',$name,'").load("Games/game_detail.php",{gameid:',$gameid,'})\' type="button" />
					';
					if($owner == $_SESSION['USERNUM']){
					echo '
					<img class="pointer" src="images/icons/edit.png" alt="Edit" style=" margin: 4px;" onClick=\'$("#',$name,'").load("Games/game_edit.php",{gameid:',$gameid,', showtype:"',$type,'"})\' type="button" />';
					}
	echo '			</div>
				
				<div id="',$name,'">
				</div>';
			}
		}	
		if($unvoted && $type == 'UNVOTE'){
				echo '<div id="game_anouncement">You have voted for all games!<br></div>';
		}else if($unvoted && $type == 'ME'){
				echo '<div id="game_anouncement">You don\'t have any games yet!<br></div>';
		}else if($unvoted){
				echo '<div id="game_anouncement">No games here!<br></div>';
		}

}	
	
function update_gamelists(){
	dblogin();

	$find_playable=
	"select count(p.player_id) as \"GOOD_CNT\"
	   from PLAYERS p 
	  where p.status = 'G'";
	if($resultf = mysql_query($find_playable)){
		if($rowf = mysql_fetch_array($resultf)){
			$playable = $rowf['GOOD_CNT'];
		}		
	}
	mysql_query("drop table VOTES;");
	$temptable =
	"create table VOTES 
	(select g.gameid, ifnull(y.good_votes,0) as \"GOOD_VOTES\", ifnull(y.dc_votes, 0) as \"DC_VOTES\",
			g.status as \"OLD_STAT\",
			case when g.status = 'D' then 'D' 
				 when ifnull(y.good_votes,0) > ($playable - ifnull(y.dc_votes,0))/2 then 'P'
				 else '?'
			end as \"NEW_STAT\", $playable as \"PLAYERS\" 
	from GAMES g
	left outer join
	(select v.gameid, 
	      sum(case when v.vote = 'Y' then 1 
	               else 0
	           end) as \"GOOD_VOTES\",
		  sum(case when v.vote = '?' then 1
				   else 0
			   end) as \"DC_VOTES\"
	from GAME_VOTE v
	group by v.gameid) y
	  on g.gameid=y.gameid)";
	mysql_query($temptable);
	
	$update=
	"update GAMES g, VOTES v
	    set g.status = v.new_stat
	 where g.gameid = v.gameid";
	mysql_query($update);

}	
	
	
function add_game_note(){
	dblogin();
	
	$insert =
	"insert into NOTES 
	 SELECT x.gid, p.player_id AS \"PID\", 
       now() AS \"DISCUSS_UPDT\", now() AS \"PLAY_UPDT\", 'N' AS \"NEW\"
	   FROM 
	      (SELECT g.gameid AS \"GID\"
			 FROM GAMES g
		     LEFT OUTER JOIN NOTES n 
			   ON g.gameid = n.gid
            WHERE n.gid IS NULL)x, PLAYERS p";
	mysql_query($insert);
}

function update_g_notes($gameid){
	dblogin();
	$insert =
	"update NOTES
	set new = 'Y'
	where gid = ".$gameid;
	mysql_query($insert);

	$insert =
	"update NOTES 
	set DISCUSS_UPDT = now()
	where gid = ".$gameid;
	mysql_query($insert);
}

function update_p_notes($gameid,$playerid){
	dblogin();	
	$insert =
	"update NOTES
	set new = 'N'
	where gid = ".$gameid."
	  and pid = ".$playerid;
	mysql_query($insert);

	$insert =
	"update NOTES
	set play_updt = now()
	where gid = ".$gameid."
	  and pid = ".$playerid;
    mysql_query($insert);
}


	
?>
	